package com.futures.questionbank.main;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;


import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.futures.questionbank.Constants.MessageEvent;
import com.futures.questionbank.R;
import com.futures.questionbank.Utils.UItils;
import com.futures.questionbank.adapter.BaseViewHolder;
import com.futures.questionbank.adapter.SectionAdpter;
import com.futures.questionbank.base.BaseActivity;
import com.futures.questionbank.db.Constants;
import com.futures.questionbank.db.table.problem;
import com.futures.questionbank.db.table.problem_Table;
import com.futures.questionbank.db.table.section;
import com.futures.questionbank.db.table.section_Table;
import com.raizlabs.android.dbflow.sql.language.SQLite;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.DecimalFormat;
import java.util.List;

public class SectionActivity extends BaseActivity {
    private RecyclerView rv;
    private List<section> sections1;


    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.section_activity);
        setTitle("章节练习");
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void loadData() {
        initView();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent messageEvent) {
        if (messageEvent.getMessageKey() != null) {
            if (messageEvent.getMessageKey().equals(Constants.FAMILY)) {
               int value = messageEvent.getMessageValue();
              //  List<problem> problems = SQLite.select().from(problem.class).where(problem_Table.exam_id.eq(value)).queryList();
               /* int i = 0;
                for (problem problem : problems) {
                    if(!TextUtils.isEmpty(problem.getMy_answer())){
                        i++;
                    }
                }**/
               // Log.i("SectionActivity----",i + "");
             //   int b = problems.size();
                DecimalFormat df = new DecimalFormat("#.00");
             //   String a = df.format((float)i/b);
              //  String a = (i/b)*100 + "";
                SectionAdpter chapterAdpter = new SectionAdpter(this, sections1,value, new TypeItemListener());
                rv.setAdapter(chapterAdpter);
            }
        }
    }

    private void initView() {
        UItils.showDialog(SectionActivity.this);
        int chapter_id = getIntent().getIntExtra("index",0);
        sections1 = SQLite.select().
                from(section.class).where(section_Table.chapter_id.eq(chapter_id)).queryList();

       section section = SQLite.select().
                from(section.class).where(section_Table.chapter_id.eq(chapter_id)).querySingle();

       // List<problem> problems = SQLite.select().from(problem.class).where(problem_Table.exam_id.eq(section.getExam_id())).queryList();
        int i = 0;
      /*  for (problem problem : problems) {
            if(!TextUtils.isEmpty(problem.getMy_answer())){
                i++;
            }
        }*/
        DecimalFormat df = new DecimalFormat("0.00");//设置保留位数
        Log.i("SectionActivity----",i + "");
      //  int b = problems.size();
       // String a = df.format((float)i/b);
        rv =  findViewById(R.id.rv);
        rv.setLayoutManager(new LinearLayoutManager(this));
        //设置Item增加、移除动画
        rv.setItemAnimator(new DefaultItemAnimator());
        SectionAdpter chapterAdpter = new SectionAdpter(this, sections1, section.getExam_id(), new TypeItemListener());
        rv.setAdapter(chapterAdpter);
    }

    class TypeItemListener implements BaseViewHolder.onItemCommonClickListener {
        @Override
        public void onItemClickListener(int position) {
            Intent intent = new Intent();
            intent.putExtra("index", sections1.get(position).getExam_id());
            intent.setClass(SectionActivity.this,ExamActivity.class);
            startActivity(intent);
        }

        @Override
        public void onItemLongClickListener(int position) {

        }
    }
}
